Data Visualization and Publication¶

This practical lab demonstrates how we can plot our data points, visualize it using python libraries such as Matplotlib,Seaborn, Plotly express and publish it as github page.

Pie Chart using Matplotlib¶

The Pie chart below shows list of ingredients and it's coresponding measure in grams or percentage required to bake a pie crust.

Ingredients¶

  • Flour - 375g
  • Sugar - 75g
  • Butter - 250g
  • Berries - 300g

Image of a  Freshly baked Pie Crust

In [1]:
import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(figsize=(6, 3), subplot_kw=dict(aspect="equal"))

recipe = ["375 g flour",
          "75 g sugar",
          "250 g butter",
          "300 g berries"]

data = [float(x.split()[0]) for x in recipe]
ingredients = [x.split()[-1] for x in recipe]


def func(pct, allvals):
    absolute = int(np.round(pct/100.*np.sum(allvals)))
    return f"{pct:.1f}%\n({absolute:d} g)"


wedges, texts, autotexts = ax.pie(data, autopct=lambda pct: func(pct, data),
                                  textprops=dict(color="w"))

ax.legend(wedges, ingredients,
          title="Ingredients",
          loc="center left",
          bbox_to_anchor=(1, 0, 0.5, 1))

plt.setp(autotexts, size=8, weight="bold")

ax.set_title("Matplotlib bakery: A pie")

plt.show()

Time Series Plot using Seaborn¶

Time Series Plot is a type of graph that displays data points collected in a time sequence.We have used FMRI dataset. FMRI stands for Functional Magnetic Resonance Imaging measures brain activity by detecting changes associated with blood flow.

Here is the link to a paper on Time series analysis of fMRI data: Spatial modelling and Bayesian computation .

In [2]:
import seaborn as sns
sns.set_theme(style="darkgrid")

# Load an example dataset with long-form data
fmri = sns.load_dataset("fmri")

# Plot the responses for different events and regions
sns.lineplot(x="timepoint", y="signal",
             hue="region", style="event",
             data=fmri)
Out[2]:
<Axes: xlabel='timepoint', ylabel='signal'>

Bar Chart using Plotly Express¶

The below bar chart shows the number of medals won by countries such as South Korea, China and Canada.

Nation Gold Silver Bronze
South Korea 24 13 11
China 10 15 8
Canada 9 12 12
In [3]:
import plotly.offline as po
import plotly.express as px
po.init_notebook_mode()
df = px.data.medals_long()

fig = px.bar(df, x="medal", y="count", color="nation",
             pattern_shape="nation", pattern_shape_sequence=[".", "x", "+"])
fig.show()